package com.pentofab.payrollmanagement.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;













import java.util.LinkedHashMap;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import com.pentofab.payrollmanagement.dao.AttendanceDao;
import com.pentofab.payrollmanagement.dao.model.Attendance;
import com.pentofab.payrollmanagement.dao.model.Employee;
import com.pentofab.payrollmanagement.database.AttendanceRepository;
import com.pentofab.payrollmanagement.database.LeaveRepository;
import com.pentofab.payrollmanagement.database.impl.AttendanceRepositoryImpl;
import com.pentofab.payrollmanagement.database.impl.LeaveRepositoryImpl;
import com.pentofab.payrollmanagement.exception.BusinessException;
import com.pentofab.payrollmanagement.exception.DatabaseException;
import com.pentofab.payrollmanagement.exception.ExceptionMsg;
import com.pentofab.payrollmanagement.utilities.CustomTableConstants;
import com.pentofab.payrollmanagement.utilities.PayRollUtility;

public class AttendanceDaoImpl implements AttendanceDao{

	private AttendanceRepository payRollRepository = null;
	private LeaveRepository leaveRepository =null;
	
	public AttendanceDaoImpl(){
		payRollRepository = new AttendanceRepositoryImpl();
		leaveRepository = new LeaveRepositoryImpl();
		System.out.println();
	}
	

	@Override
	public void addAttandeanceEntry(String empId, Date date,
			String loginTime,String logOutTime,String createdEmp,int rowNum,boolean firstIn,boolean lastOut) throws BusinessException {
		
		loginTime = loginTime.replace(".", ":");
		logOutTime = logOutTime.replace(".", ":");
		System.out.println("loginTime : "+loginTime);
		System.out.println("logOutTime : "+logOutTime);
		PayRollUtility.validateEmpId(empId);
		String currentDate = PayRollUtility.convertDateToString(date);
		System.out.println("currentDate : "+currentDate);
		PayRollUtility.validateLoginTime(loginTime);
		
		try{
			if(leaveRepository.empOnLeave(empId,PayRollUtility.convertDateToString(date))){
				throw new BusinessException(String.format(ExceptionMsg.EMPLOYEE_ON_LEAVE, empId));
			}
			
		   payRollRepository.addAttandeanceEntry(empId, currentDate, loginTime, logOutTime, createdEmp,rowNum,firstIn,lastOut);
		   
		}catch (MySQLIntegrityConstraintViolationException exception) {
			throw new BusinessException(exception, ERROR_WHILE_ADDING_ATTANDANCE_INFO+empId+" for in time"+loginTime+", out time "+logOutTime);
		}catch(BusinessException exception){
			
			throw new BusinessException(String.format(ExceptionMsg.EMPLOYEE_ON_LEAVE, empId));
		}catch(Exception exception){
			throw new BusinessException(exception, ERROR_WHILE_ADDING_ATTANDANCE_INFO+empId+" for in time"+loginTime+", out time "+logOutTime);
		}
		
	}

	@Override
	public void setlogOutTime(Attendance latestAttEntry)
			throws BusinessException {

		PayRollUtility.validateAttandance(latestAttEntry);
		
		try{
			payRollRepository.setlogOutTime(latestAttEntry.getLogoutTime(), 
					latestAttEntry.getEmployee().getEmpId(), latestAttEntry.getRownumber());
		}catch(Exception exception){
			throw new BusinessException(exception,ERROR_WHILE_UPDATING_LOGOUT_INFO+
					latestAttEntry.getEmployee().getEmpId());
		}
	}

	@Override
	public void  modifyAttendance(String empId,String date,String intime, 
			boolean first, String outTime, boolean last, int rowno)
			throws BusinessException {

		ArrayList<Attendance> attendanceInfo = null;
		try {
			attendanceInfo = payRollRepository.getAttandaceInfo(empId, date);
			
			System.out.println("attendanceInfo ; "+attendanceInfo);
			System.out.println("empId : "+empId);
			System.out.println("rowno : "+rowno);
			System.out.println("date : "+date);
			for (Attendance current : attendanceInfo) {
				if(empId.equals(current.getEmployee().getEmpId()) && 
						rowno == current.getRownumber() && date.equals(current.getDate()) ){
					
					System.out.println("modify DB CALL");
					payRollRepository.modifyAttendance(empId, date, intime, first,
							outTime, last, rowno);
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(e,ERROR_WHILE_MODIFYING_ATTANDANCE_INFO);
		}
	}

	@Override
	public ArrayList<Attendance> getAttandaceInfo(String empId, String date)
			throws BusinessException {
		// TODO Auto-generated method stub
		PayRollUtility.validateEmpId(empId);
		try {
			ArrayList<Attendance> attandanceList = 	payRollRepository.getAttandaceInfo(empId, date);
			if(attandanceList == null){
                            return null;
//				throw new BusinessException(NO_ATTANDANCE_ENTRY+empId);
			}
			return attandanceList;
		} catch (Exception exception) {
			throw new BusinessException(exception,
					ERROR_WHILE_RETRIVING_ATTANDANCE_INFO+empId);
		}
	}


	@Override
	public void updateLogOutStatus(String empId, String date, boolean status) throws BusinessException {
		try {
			payRollRepository.updateLogOutStatus(empId,date, status);
		} catch (Exception e) {
			throw new BusinessException(e,
					ERROR_WHILE_UPDATIN_LOGOUT_INFO+empId);
		}
	}


	@Override
	public  ArrayList<HashMap<String,String>> employeeTimesheetInfo(String empId,String companyCode,
			String department,String from_date,String to_date,String unit, int mode)
			throws BusinessException {

			if(PayRollUtility.compareTwoDates(from_date, to_date) >0){
				throw new BusinessException(from_date+ExceptionMsg.TO_DATE_BEFORE_FROM_DATE+to_date);
			}
			
			try{
			return payRollRepository.employeeTimesheetInfo(
					empId, companyCode,department, from_date,to_date,unit, mode);
			}catch(Exception e){
				throw new BusinessException(e,ExceptionMsg.ERROR_WHILE_RETRIVING_ATTANDANCE_INFO);
			}
	}


	@Override
	public void deleteAttEntry(String empId, int rowNum, String date) throws BusinessException {
		try{
		payRollRepository.deleteAttEntry(empId,rowNum,date);
		}catch(Exception e){
			
		}
	}


	@Override
	public void updateRowNum(int rowNum,String empId, String loginTime, String logOutTime,
			String date) throws BusinessException {
		
		try{
			payRollRepository.updateRowNumber(rowNum,empId, loginTime, logOutTime, date);
			}catch(Exception e){
				throw new BusinessException(e,e.getMessage());
			}
		
	}


	@Override
	public LinkedHashMap<String, ArrayList<Attendance>> getAttEntry(String date,String company,String unit, String dept) throws BusinessException {
		try{
			return payRollRepository.getAttEntry( date, company, unit,  dept);
		}catch(Exception e){
			throw new BusinessException(e,e.getMessage());
		}
	}
	
}
